package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping("/getUsername")
    public Result getUsername(){
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        System.out.println("用户名是:"+username);
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<User> result =null;
        try {
            PageInfo<User> userPageInfo = userService.findPage(queryPageBean);
            result = new PageResult<>(userPageInfo.getTotal(),userPageInfo.getList());
            System.out.println(result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取用户失败");
        }
        return new Result(true, "获取用户信息成功",result);
    }



    //findAll
    @PostMapping("/findAll")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findAll(){
        List<Role> roleList = userService.findRole();
        return new Result(true, "获取用户菜单成功",roleList);
    }


    //userIds

    @PostMapping("/add")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody User user,Integer[] roleIds){
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userService.add(user,roleIds);
        return new Result(true, "添加用户成功");
    }

    @PostMapping("/edit")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        userService.edit(user,roleIds);
        return new Result(true, "编辑用户成功");
    }


    @GetMapping("/findUserById")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findRoleById(Integer userId){
        List<Integer> roleIds = userService.findUserById(userId);
        return new Result(true, "查找用户信息成功",roleIds);
    }

    @PostMapping("/deleteById")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result deleteById(Integer id){
        userService.deleteById(id);
        return new Result(true, "删除用户成功");
    }

    @GetMapping("/getMemu")
    public Result getMemu(){
        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = principal.getUsername();
            List<Menu> list = menuService.getMenus(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }


}
